package com.liangzong.betspider.service;

import com.liangzong.betspider.entity.TeamInfo;
import com.liangzong.betspider.entity.TeamPoint;
import com.liangzong.betspider.entity.TeamScore;
import com.liangzong.betspider.enums.TeamTypeEnum;
import com.liangzong.betspider.po.QueryCondition;
import com.liangzong.betspider.po.TeamInfoResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class FindDataService {
    @Autowired
    TeamInfoService teamInfoService;

    @Autowired
    TeamPointService teamPointService;

    @Autowired
    TeamScoreService teamScoreService;


    public List<TeamInfoResult> query(List<QueryCondition> conditions) {
        List<TeamInfo> teamInfoList = teamInfoService.query();
        List<TeamInfoResult> results = new ArrayList<>();
        Set<String> set = new HashSet<>();
        for (TeamInfo teamInfo : teamInfoList) {
            String id = teamInfo.getId();
            if (set.contains(id)) {
                continue;
            }
            String time = teamInfo.getTime();

            String[] split = time.split(":");
            Integer minutes = Integer.parseInt(split[0]);
            Integer shootScore = null;
            for (QueryCondition condition : conditions) {
                Integer queryTime = condition.getTime();
                Integer globalRight = condition.getGlobalRight();
                BigDecimal startWaterLevel = condition.getStartWaterLevel();
                BigDecimal endWaterLevel = condition.getEndWaterLevel();
                if(shootScore == null) {
                    shootScore = condition.getShootScore();
                }
                //判断赔率
                //水位是否满足
                boolean homeTeamWaterLevel = false;
                boolean awayTeamWaterLevel = false;
                //是否平盘或受让
                boolean homeTeamHd = false;
                boolean awayTeamHd = false;
                List<TeamPoint> teamPointList = teamPointService.getByTeamId(id);
                for (TeamPoint teamPoint : teamPointList) {
                    String hd = teamPoint.getHd();
                    BigDecimal odds = teamPoint.getOdds();
                    boolean isOdds = odds.compareTo(startWaterLevel) >= 0 && odds.compareTo(endWaterLevel) <= 0;
                    if (teamPoint.getTeamType() == TeamTypeEnum.HOME.getType()) {
                        if (!homeTeamHd && !homeTeamWaterLevel && hd.indexOf("-") == -1) {
                            if (isOdds) {
                                homeTeamWaterLevel = true;
                                homeTeamHd = true;
                            }
                        }
                    }
                    if (teamPoint.getTeamType() == TeamTypeEnum.AWAY.getType()) {
                        if (!awayTeamHd && !awayTeamWaterLevel && hd.indexOf("-") == -1) {
                            if (isOdds) {
                                awayTeamWaterLevel = true;
                                awayTeamHd = true;
                            }
                        }
                    }
                }
                //球权
                int homeTeamGlobalRight = 0;
                int awayTeamGlobalRight = 0;
                //判断射正+射偏
                int homeTeamShootScore = 0;
                int awayTeamShootScore = 0;
                List<TeamScore> teamScoreList = teamScoreService.getByTeamId(id);
                for (TeamScore teamScore : teamScoreList) {
                    if (teamScore.getTeamType() == TeamTypeEnum.HOME.getType()) {
                        homeTeamShootScore += teamScore.getS1() + teamScore.getS2();
                        homeTeamGlobalRight = teamScore.getS7();
                    } else {
                        awayTeamShootScore += teamScore.getS1() + teamScore.getS2();
                        awayTeamGlobalRight = teamScore.getS7();
                    }
                }
                TeamInfoResult teamInfoResult = null;
                //策略1 射正+射偏 >= shootScore
                int matchShootScore = homeTeamShootScore - awayTeamShootScore;
                boolean matched = false;
                if (shootScore != null) {
                    if (Math.abs(matchShootScore) >= shootScore && matchShootScore > 0 && homeTeamWaterLevel && homeTeamHd) {
                        //主队满足
                        log.info("策略1 主队 matchShootScore=" + matchShootScore + "#homeTeamWaterLevel=" + homeTeamWaterLevel + "#homeTeamHd=" + homeTeamHd);
                        teamInfoResult = new TeamInfoResult();
                        teamInfoResult.setHomeTeamName(teamInfo.getHomeTeamName());
                        teamInfoResult.setAwayTeamName(teamInfo.getAwayTeamName());
                        teamInfoResult.setGlobalRight(homeTeamGlobalRight);
                        teamInfoResult.setShootScore(matchShootScore);
                        matched = true;
                    } else if (Math.abs(matchShootScore) >= shootScore && matchShootScore < 0 && awayTeamWaterLevel && awayTeamHd) {
                        //客队满足
                        log.info("策略1 客队 matchShootScore=" + matchShootScore + "#awayTeamWaterLevel=" + awayTeamWaterLevel + "#awayTeamHd=" + awayTeamHd);
                        teamInfoResult = new TeamInfoResult();
                        teamInfoResult.setHomeTeamName(teamInfo.getHomeTeamName());
                        teamInfoResult.setAwayTeamName(teamInfo.getAwayTeamName());
                        teamInfoResult.setGlobalRight(-1 * awayTeamGlobalRight);
                        teamInfoResult.setShootScore(matchShootScore);
                        matched = true;
                    }
                }
                //策略2 比赛时长 球权 射门
                if (!matched && queryTime != null && globalRight != null) {
                    if (queryTime != null && minutes > queryTime) {
                        //球权 时长 盘口水位 平盘  射门默认不落后1球
                        if (homeTeamGlobalRight >= globalRight && homeTeamWaterLevel && homeTeamHd && matchShootScore >= shootScore) {
                            //主队满足
                            log.info("策略2 主队 homeTeamGlobalRight=" + homeTeamGlobalRight + "#homeTeamWaterLevel=" + homeTeamWaterLevel + "#homeTeamHd=" + homeTeamHd + "#matchShootScore=" + matchShootScore);
                            teamInfoResult = new TeamInfoResult();
                            teamInfoResult.setHomeTeamName(teamInfo.getHomeTeamName());
                            teamInfoResult.setAwayTeamName(teamInfo.getAwayTeamName());
                            teamInfoResult.setGlobalRight(homeTeamGlobalRight);
                            teamInfoResult.setShootScore(matchShootScore);
                        } else if (awayTeamGlobalRight >= globalRight && awayTeamWaterLevel && awayTeamHd && matchShootScore <= shootScore) {
                            //客队满足
                            log.info("策略2 客队 awayTeamGlobalRight=" + awayTeamGlobalRight + "#homeTeamWaterLevel=" + homeTeamWaterLevel + "#homeTeamHd=" + homeTeamHd + "#matchShootScore=" + matchShootScore);
                            teamInfoResult = new TeamInfoResult();
                            teamInfoResult.setHomeTeamName(teamInfo.getHomeTeamName());
                            teamInfoResult.setAwayTeamName(teamInfo.getAwayTeamName());
                            teamInfoResult.setGlobalRight(-1 * awayTeamGlobalRight);
                            teamInfoResult.setShootScore(matchShootScore);
                        }
                    }
                }

                if (teamInfoResult != null) {
                    if (set.add(id)) {
                        teamInfoResult.setUrl(teamInfo.getUrl());
                        teamInfoResult.setId(teamInfo.getId());
                        teamInfoResult.setTime(teamInfo.getTime());
                        results.add(teamInfoResult);
                    }
                }
            }
        }
        return results;
    }
}
